<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

global $_CONF;
$_CONF['debug'] = false;

App::loadAjax(array('Chat', 'Smiles'), array
(
	'getChatText',
	'processForm',
	'getOnlineUser',
	'loadEmoticons'
));

if ( !isset($_POST['xajax']) )
{
	App::membersOnly();
	
	$oTpl =& App::getTemplate();
	$oReq =& App::getRequest();
	$oDb =& Database::get();

	$iRoomId = $oReq->getInt('roomid', 1);

	$aRooms = $oDb->getRows("SELECT roomid, name FROM ". App::getT('chat_rooms') ." ORDER BY name ASC");

	$iLimitUsers = App::getParam('iMaxOnlineChatUsers');
	$iNowUsers = updateChatOnLine();
	if( $iLimitUsers>0 AND ($iLimitUsers<=$iNowUsers) )
	{
		$oTpl->assignSrc(array(
	    'bMembersLimit' => 1,
	    ));
	    $oTpl->setTemplateFile('index.html');
	}else{
		
	$sUserFileName = App::getParam('chat.path').'users'.$iRoomId.'.txt';
	if(file_exists($sUserFileName))
	{
		$aRows = unserialize(file_get_contents($sUserFileName));
	}else{
		$aRows = array();
	}
	$aRows[App::getUser('user')] = time();
	if($hFile = fopen($sUserFileName,'w'))
	{
		fwrite($hFile, serialize($aRows));
		fclose($hFile);
	}
	
	$sJavaScript = '<script type="text/javascript">
		function removeChatSession()
		{
			// oAjax.post("Chat", "deleteChatSession");
			opener.window.document.getElementById(\'ajax_temp_content\').innerHTML=\'<iframe frameborder="0" width="0" height="0" src="' . App::getParam('url') . 'design/static/ajax.php?intelajax[module]=Chat&intelajax[function]=deleteChatSession"><\/iframe>\';
		}
		window.onunload = removeChatSession;
	</script>';
	$sJavaScript .= "<script type=\"text/javascript\">\n\t\twindow.onload=clicktoHideAll;\n\t\tvar sDisplayIds = new Array('mainEmoticons');\n</script>";
		
	$oTpl->assignSrc(array(
	    'iRoomId' => $iRoomId,
	    'aRooms' => $aRooms,
	    'sRootPath' => $oTpl->template_dir,
	    'sChatContent' => getChatText(1, $iRoomId, false, false),
	    'sWhosOnline' => getOnlineUser($iRoomId, false),
	    'sExtraHeader' => '' .  $sJavaScript . '<script type="text/javascript" src="design/js/tinymce/tiny_mce.js"></script>
		<script type="text/javascript">
		tinyMCE.init({
			mode : "exact",
			elements : "maintext",
			theme : "advanced",
			plugins : "enter",
			theme_advanced_buttons1 : "bold,italic,underline,separator,cut,copy,paste,separator,link,unlink,fontselect,fontsizeselect,separator,forecolor",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
	        theme_advanced_toolbar_location : "top",
	        theme_advanced_toolbar_align : "center",
	        theme_advanced_statusbar_location : "bottom",
	        cleanup : false,
	        convert_newlines_to_brs : false,
	        force_br_newlines : true,
	        theme_advanced_resizing : true,
	        theme_advanced_resize_horizontal : true,
	        theme_advanced_more_colors : false,
			convert_urls : false,
			relative_urls : true		
		});</script>'
	));

	$oTpl->setTemplateFile('index.html');
	}
}

function updateChatOnLine()
{
	$sDir = App::getParam('chat.path');
	$iCountUsers = 0;
	if ($dh = opendir($sDir)) 
	{
        while (($sFile = readdir($dh)) !== false) 
       	{
            if('users'==substr($sFile,0,5))
            {
	            $aRows = unserialize(file_get_contents($sDir.$sFile));
            	foreach($aRows as $sUser=>$iTime)
		        {
		            if($sUser==App::getUser('user'))
		            {
		                unset($aRows[$sUser]);
                        if($hFile = fopen($sDir.$sFile,'w'))
                        {
                            fwrite($hFile, serialize($aRows));
                            fclose($hFile);
                        }
                        break;
		            }
		        }
		        $iCountUsers += count($aRows);
			}
        }
        closedir($dh);
    }
    return $iCountUsers;
}
?>